from django.contrib.auth.models import AbstractUser
from django.db import models

from PAA_ERP.utils.dase_model import BaseModel


# Create your models here.
class MenuModel(BaseModel):
    number = models.IntegerField('排序数字', blank=True, null=True, )
    url = models.CharField('菜单访问的url地址', max_length=256, blank=True, null=True, )
    name = models.CharField('菜单的名字', max_length=50)
    delete_flag = models.CharField('删除的标记', max_length=1, default='0')  # 不会真的删除数据
    parent = models.ForeignKey('self', blank=True, null=True, related_name='children', on_delete=models.CASCADE)

    class Meta:
        db_table = 't_menu'
        verbose_name = '功能菜单表'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.name


# ERP系统的操作用户模型类，默认是采用django自带的User，需要配置settings
class UserModel(AbstractUser):
    phone = models.CharField('手机号码', blank=True, null=True, max_length=11, unique=True)
    real_name = models.CharField('真实姓名', max_length=128, blank=True, null=True)
    roles = models.ManyToManyField('RolesModel', db_table='t_users_roles', blank=True)
    dept = models.ForeignKey('DeptModel', blank=True,null=True,on_delete=models.SET_NULL,verbose_name='用户所在部门')

    class Meta:
        db_table = 't_user'
        verbose_name = '用户模型'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.username


class PermissionsModel(BaseModel):
    """
    权限模型
    """
    method_choices = (
        (u'POST', u'增'),
        (u'DELETE', u'删'),
        (u'PUT', u'改'),
        (u'PATCH', u'局部改'),
        (u'GET', u'查'))
    '''
    只要是choices参数的字段，如果你想要收获对应的key和value，默认获取的是key,
    如果需要获取value，可以采用get_字段名字_display()
    print(permission.method) 获取key
    print(permission.get_method_display()) 获取value
    
    '''

    name = models.CharField(max_length=50, verbose_name='权限名')
    is_menu = models.BooleanField(verbose_name='是否为菜单', default=True)  # True为菜单,False为接口
    # 操作
    method = models.CharField(max_length=8, blank=True, default='', choices=method_choices, verbose_name='请求方法')
    path = models.CharField(max_length=200, blank=True, default='', verbose_name='请求路径')
    # 资源
    menu = models.ForeignKey('MenuModel', null=True, blank=True, related_name='permissions_list', on_delete=models.CASCADE)
    desc = models.CharField(max_length=512, blank=True, default='', verbose_name='权限描述')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 't_permissions'
        verbose_name = '权限'
        verbose_name_plural = verbose_name
        ordering = ['id']


class RolesModel(BaseModel):
    """
    角色
    """
    name = models.CharField(max_length=32, unique=True, verbose_name='角色名字')
    permissions = models.ManyToManyField('PermissionsModel', db_table='t_roles_permissions', blank=True,
                                         verbose_name='权限')

    # 注意在用户模型中最好也要加ManyToManyField
    def __str__(self):
        return self.name

    class Meta:
        db_table = 't_roles'
        verbose_name = '角色'
        verbose_name_plural = verbose_name
        ordering = ['id']


class DeptModel(BaseModel):
    """
    用户所在部门的模型
    """
    name = models.CharField('部门名称',max_length=50, unique=True)
    address = models.CharField('部门所在地址',max_length=256, null=True,blank=True)
    parent = models.ForeignKey('self',null=True,blank=True,related_name='children',on_delete=models.CASCADE,verbose_name='部门所在的父部门')


    # 注意在用户模型中最好也要加ManyToManyField
    def __str__(self):
        return self.name

    class Meta:
        db_table = 't_dept'
        verbose_name = '部门'
        verbose_name_plural = verbose_name
        ordering = ['id']

